﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using DNA.Mvc.Models;

namespace DNA.Mvc.UI
{
    public class WidgetHelper
    {
        private Dictionary<string, object> userPreferences;
        private WidgetInstance instance;
        private Dictionary<string, PropertyDescriptor> propertyDescriptors = null;
        public Dictionary<string, PropertyDescriptor> PropertyDescriptors
        {
            get { return propertyDescriptors; }
            internal set { propertyDescriptors = value; }
        }

        public string IDPrefix
        {
            get
            {
                if (instance != null)
                {
                    return instance.ID.ToString().Substring(0, 5);
                }
                return "";
            }
        }

        public WidgetInstance Model { get { return instance; } internal set { instance = value; } }

        /// <summary>
        /// Return the integer value from UserPreferences by specified key.
        /// </summary>
        /// <param name="key">The user preference key name.</param>
        /// <returns>The integer value</returns>
        public int GetInt(string key)
        {
            return GetData<int>(key);
        }

        /// <summary>
        /// Returns the boolean value forom UserPreferences by specified key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool GetBool(string key)
        {
            return GetData<bool>(key);
        }

        /// <summary>
        /// Returns the string value forom UserPreferences by specified key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetString(string key)
        {
            return GetData<string>(key);
        }

        public T GetData<T>(string key)
        {
            if (UserPreferences.ContainsKey(key))
            {
                return (T)UserPreferences[key];
            }

            return default(T);
        }

        /// <summary>
        /// Returns the decimal value forom UserPreferences by specified key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public decimal GetDecimal(string key)
        {
            return GetData<Decimal>(key);
        }

        public Dictionary<string, object> UserPreferences
        {
            get { return userPreferences; }
            internal set { userPreferences = value; }
        }

        public string GenerateFieldID(string fieldName)
        {
            return fieldName + IDPrefix;
        }

        /// <summary>
        /// Get the widget definition package path 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string GetWidgetPath(string path)
        {
            if (path.StartsWith("~") || path.StartsWith("/"))
                throw new Exception("The path could not be start with \"~\" or \"\"/");

            if (Model != null)
            {
                return UrlUtility.CreateUrlHelper().Content("~/content/widgets/" + Model.WidgetDescriptor.InstalledPath.Replace("\\", "/") + "/" + path);
            }

            return "";
        }
    }
}